﻿using AutoMapper;
using CSRedis;
using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Services
{
    public class UnitService:IUnitService
    {

        private readonly IUnitRepository unitRe;
        private readonly ILogger<UnitService> logger;
        private readonly IMapper imapper;
        public CSRedisClient _csreCilent;

        public UnitService(IUnitRepository unitRe, ILogger<UnitService> logger, IMapper imapper)
        {
            this.unitRe = unitRe;
            this.logger = logger;
            this.imapper = imapper;
            _csreCilent = new CSRedisClient("127.0.0.1:6379");
        }

        /// <summary>
        /// 省列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<Province>> ProvinceList()
        {
            try
            {    logger.LogInformation("省列表");
                return await unitRe.ProvinceList();
            }
            catch (Exception ex)
            {

                logger.LogError(ex, ex.Message);
                throw;
            }
          
        }


        /// <summary>
        /// 市列表
        /// </summary>
        /// <param name="provinceId"></param>
        /// <returns></returns>
        public async Task<List<City>> CityList(int provinceId)
        {
            try
            {    logger.LogInformation("市列表");
                return await unitRe.CityList(provinceId);
            }
            catch (Exception ex)
            {

                logger.LogError(ex, ex.Message);
                throw;
            }
           

        }


        /// <summary>
        /// 区列表
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
        public async Task<List<Area>> UnitList(int cityId)
        {
            try
            {    logger.LogInformation("区列表");
                return await unitRe.UnitList(cityId);
            }
            catch (Exception ex)
            {

                logger.LogError(ex, ex.Message);
                throw;
            }
        

        }


        /// <summary>
        /// 单位添加
        /// </summary>
        /// <param name="unit"></param>
        /// <returns></returns>
        public async Task<int> Add(Unit unit)
        {
            try
            {
                logger.LogInformation("添加单位");
                var entity = imapper.Map<Unit>(unit);
                return await unitRe.UnitAdd(entity);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                throw;
            }
        

        }


        /// <summary>
        /// 单位查询分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageOutput<UnitListOutPut>> GetUnitList(UnitListInPut input)
        {

            var list = await unitRe.Unit();
            if (!string.IsNullOrEmpty(input.CompanyName))
            {
                list = list.Where(c => c.CompanyName.Contains(input.CompanyName));
            }
           
            var result = new PageOutput<UnitListOutPut>
            {
                TotalCount = list.Count(),
                PageSize = input.PageSize,
            };

            list = list.OrderBy(c => c.CompanyId).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
            result.Data = imapper.Map<List<UnitListOutPut>>(list);
            return result;

        }







    }


}
